package com.yumeng.framework.core.mvc.interceptor;

import com.yumeng.common.runtime.ThreadLocalHelper;
import com.yumeng.framework.persistence.mybatis.plugins.dynamictable.DynamicTableHelper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * ThreadLocal 清理
 *
 * @author wxd
 * @date 2025/10/31 16:45
 */
@Slf4j
public class ThreadLocalInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("ThreadLocalInterceptor::preHandle");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.debug("ThreadLocalInterceptor::postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.debug("ThreadLocalInterceptor::afterCompletion");
        //检查ThreadLocalHelper是否及时清理
        ThreadLocalHelper.checkThreadLocalHelper();
        //检查本地存储参数是否及时清理
        DynamicTableHelper.checkLocalParamList();
    }
}
